package com.techysky.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.techysky.entity.MultiMedia;
import com.techysky.repository.DataModuleRepository;
import com.techysky.repository.MultiMediaRepository;
import com.techysky.service.MultiMediaService;
import com.techysky.util.CoreUtils;

@Service("multiMediaService")
public class MultiMediaServiceImpl implements MultiMediaService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private transient MultiMediaRepository multiMediaRepository;
	@Autowired
	private transient DataModuleRepository dataModuleRepository;

	@Override
	public Object saveOrUpdate(MultiMedia multiMedia) {
		return this.multiMediaRepository.save(multiMedia);
	}
	@Override
	public Map<String,Object> query(MultiMedia multiMedia) {
		Map<String,Object> result=new HashMap<>();
		multiMedia.setPage(multiMedia.getPage()!=null?multiMedia.getPage():0);
		multiMedia.setSize(multiMedia.getSize()!=null?multiMedia.getSize():8);
		Page<MultiMedia> resultList=this.multiMediaRepository.findAll(queryConditions(multiMedia),new PageRequest(multiMedia.getPage(),multiMedia.getSize()));
		result.put("page", multiMedia.getPage()+1);
		result.put("size", multiMedia.getSize());
		result.put("search", multiMedia.getTitle());
		result.put("rows", CoreUtils.getInstance().convert(resultList.getContent()));
		result.put("total", resultList.getTotalElements());
		return result;
	}

	/**
	 * 等值匹配条件
	 * @return
	 */
	public Specification<MultiMedia> queryConditions(final MultiMedia media){
		return (Root<MultiMedia> root,CriteriaQuery<?> query, CriteriaBuilder cb)->{
            List<Predicate> predicate = new ArrayList<>();
            predicate.add(cb.isNotNull(root.get("title")));
            In<String> in=cb.in(root.get("extName"));
            in.value(".avi").value(".AVI").value(".swf").value(".SWF").value("wrl").value(".WRL").value(".unity3d").value(".UNITY3D").value(".wmv").value(".WMV").value(".mpg").value(".MPG").value(".MPEG").value(".mpeg");
            predicate.add(in);
            if(media!=null&&media.getTitle()!=null&&!"".equals(media.getTitle())) {
            		predicate.add(cb.like(root.get("title"),"%"+media.getTitle()+"%"));
		}
            Predicate[] pre = new Predicate[predicate.size()];
            return query.where(predicate.toArray(pre)).getRestriction();
		};
	}
	@Override
	public MultiMedia findByProjectIdAndCode(String projectId, String icn) {
	List<MultiMedia> multiMedias=this.multiMediaRepository.findByProjectIdAndCode(projectId,icn);
	if(!multiMedias.isEmpty()) {
		return multiMedias.get(0);
	}else return null;
	}
	@Override
	public List<MultiMedia> findByNameLike(String name) {
		return this.multiMediaRepository.findByTitleLike("%"+(StringUtils.isEmpty(name)?"":name)+"%");
	}

}
